/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.asset.contract.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.asset.constant.ContractConstant;
import com.fuyao.cloud.asset.contract.mapper.AssetContractMapper;
import com.fuyao.cloud.asset.contract.service.AssetContractService;
import com.fuyao.cloud.asset.entity.AssetContract;
import com.fuyao.cloud.asset.util.YearUtils;
import com.fuyao.cloud.asset.vo.BillExcelVO;
import com.fuyao.cloud.asset.vo.ContractExcelVO;
import com.fuyao.cloud.common.core.model.BaseEntity;
import com.fuyao.cloud.common.core.util.DictItemOption;
import com.fuyao.cloud.common.data.datascope.DataScope;
import com.fuyao.cloud.common.data.resolver.DictItemResolver;
import com.fuyao.cloud.common.data.resolver.NumberResolver;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 合同管理
 *
 * @author liyue
 * @date 2021-11-18 18:45:12
 */
@Service
public class AssetContractServiceImpl extends ServiceImpl<AssetContractMapper, AssetContract> implements AssetContractService {

	@Override
	public boolean save(AssetContract entity) {
		setFieldValue(entity);
		return super.save(entity);
	}

	@Override
	public boolean updateById(AssetContract entity) {
		setFieldValue(entity);
		return super.updateById(entity);
	}

	@Override
	public List listContract(QueryWrapper<AssetContract> query, String type) {
		// 查询字典信息
		Map<String, List<DictItemOption>> resultMap = DictItemResolver.getDictOptionResultMap(ContractConstant.CONTRACT_OPTIONS);

		List<AssetContract> contractList = this.baseMapper.selectListByScope(query, new DataScope());
		return contractList.stream().map(entity -> {
			// 发票对象
			if("BILL".equals(type)){
				BillExcelVO exportVO = new BillExcelVO();
				BeanUtils.copyProperties(entity, exportVO);
				exportVO.setStatus(DictItemResolver.getDictOptionLabel(resultMap.get(ContractConstant.CONTRACT_STATUS), entity.getStatus()));
				exportVO.setType(DictItemResolver.getDictOptionLabel(resultMap.get(ContractConstant.CONTRACT_TYPE_HOST), entity.getType()));
				exportVO.setContractType(DictItemResolver.getDictOptionLabel(resultMap.get(ContractConstant.CONTRACT_TYPE_RATE), entity.getContractType()));
				return exportVO;
			}else{
				ContractExcelVO exportVO = new ContractExcelVO();
				BeanUtils.copyProperties(entity, exportVO);
				exportVO.setStatus(DictItemResolver.getDictOptionLabel(resultMap.get(ContractConstant.CONTRACT_STATUS), entity.getStatus()));
				exportVO.setType(DictItemResolver.getDictOptionLabel(resultMap.get(ContractConstant.CONTRACT_TYPE_HOST), entity.getType()));
				exportVO.setContractType(DictItemResolver.getDictOptionLabel(resultMap.get(ContractConstant.CONTRACT_TYPE_RATE), entity.getContractType()));
				return exportVO;
			}
		}).collect(Collectors.toList());
	}

	@Override
	public List<Tree<Integer>> selectTree() {
		return YearUtils.getYearTreeList();
	}

	@Override
	public List<Tree<Long>> selectContractTree(QueryWrapper<AssetContract> query) {
		// 查询房屋信息
		List<AssetContract> contractList = this.baseMapper.selectList(query);
		// 查询数据权限内房屋信息
		List<Long> contractOwnIdList = this.baseMapper.selectListByScope(query, new DataScope()).stream()
				.map(BaseEntity::getDeptId).collect(Collectors.toList());

		// 权限内房屋信息
		List<TreeNode<Long>> collect = contractList.stream()
				.sorted(Comparator.comparingLong(AssetContract::getId)).map(contract -> {
					TreeNode<Long> treeNode = new TreeNode();
					treeNode.setId(contract.getId());
					treeNode.setParentId(0L);
					treeNode.setName(contract.getName());
					treeNode.setWeight(120);
					// 有权限不返回标识
					Map<String, Object> extra = new HashMap<>(8);
					extra.put("isLock", !contractOwnIdList.contains(contract.getDeptId()));
					extra.put("createTime", contract.getCreateTime());
					extra.put("code", contract.getContractId());

					treeNode.setExtra(extra);
					return treeNode;
				}).collect(Collectors.toList());
		return TreeUtil.build(collect, 0L);
	}

	private void setFieldValue(AssetContract entity){
		if(StrUtil.isEmpty(entity.getContractId())){
			String numberCode = ContractConstant.CONTRACT.equals(entity.getType())?ContractConstant.CONTRACT_NUMBER_CODE:ContractConstant.CONTRACT_NUMBER_CODE_BILL;
			String numberStr = (String) NumberResolver.getNumberStr(ContractConstant.CONTRACT_NUMBER_TYPE, numberCode ).getData();
			entity.setContractId(numberStr);
		}
		int year = entity.getContractSigndate().getYear();
		int monthValue = entity.getContractSigndate().getMonthValue();
		entity.setYear(year);
		entity.setMonth(monthValue);
	}



}
